using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dao;
using Entities;
using System.Collections.Generic;
namespace Doctors_WebForum.Admin.UIControls
{
    public partial class AccountsDetails : System.Web.UI.UserControl
    {
        private IList<User> listUser;
        private UserData userData;
        private User user;
        private Profile profile;
        private ProfileData profileData;
        public AccountsDetails()
        {
            listUser = new List<User>();
            userData = new UserData();
            user = new User();
            profile = new Profile();
            profileData = new ProfileData();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            string id = Request.QueryString["ID"];
            user = userData.GetById(Utility.ToInt(id));
            txtID.Text = user.UserID.ToString();
            txtUserName.Text = user.UserName;
            //txtRoleName.Text = user.RoleName;
            RoleData roleData = new RoleData();
            drRoleName.DataSource = roleData.GetAll();
            drRoleName.DataValueField = "RoleID";
            drRoleName.DataTextField = "RoleName";
            drRoleName.DataBind();
            txtEmail.Text = user.Email;

            txtCreateDate.Text = user.CreatedDate.ToShortDateString();
            if (user.IsActive == true)
            {
                chkActived.Checked = true;
                chkActived.Text = "Active";
            }
            else
            {
                chkActived.Checked = false;
                chkActived.Text = "UnActive";
            }
            txtLastLogin.Text = user.LastLogin.ToShortDateString();
            if (user.IsBan == true)
            {
                chkBan.Checked = true;
                chkBan.Text = "UnBan";
            }
            else
            {
                chkBan.Checked = false;
                chkBan.Text = "Ban";
            }
            profile = profileData.GetByUserId(Utility.ToInt(id));
            txtFullName.Text = profile.FullName;
            txtProfessional.Text = profile.Professional;
            txtQualification.Text = profile.Qualification;

            if (profile.Gender == true)
            {
                chkGender.Checked = true;
                chkGender.Text = "Male";
            }
            else
            {
                chkGender.Checked = false;
                chkGender.Text = "Famale";
            }
            txtExperience.Text = profile.Experience.ToString();
            if (profile.IsPublicProfile == true)
            {
                chkPublichProfile.Checked = true;
                chkPublichProfile.Text = "Public";
            }
            else
            {
                chkPublichProfile.Checked = false;
                chkPublichProfile.Text = "Private";
            }
            txtBirth.Text = profile.Birthday.ToShortDateString();
            imgAvatar.ImageUrl = "~/AvatarUpLoad/" + profile.AvatarPath;
            txtCountry.Text = profile.Country;
            txtAddress.Text = profile.Address;
            txtPhone.Text = profile.Phone;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.close()</script>");
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //active
            bool active;
            if (chkActived.Checked == true)
                active = true;
            else
                active = false;
            //is ban
            bool ban;
            if (chkBan.Checked == true)
                ban = true;
            else
                ban = false;

            // drpRole
            string role = drRoleName.SelectedValue;
            //id
            string id = txtID.Text;
            // 
            user.UserID = Utility.ToInt(id);
            user.IsBan = ban;
            user.IsActive = active;
            user.RoleID = Utility.ToByte(role);

            if (userData.Update(user) == true)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "update success !";
            }
            else
            {
                lblMessage.Text = "update not success !";
            }
        }

        public void Search()
        {
            user = new User();
            user.UserName = txtUserName.Text;
            user.Email = txtEmail.Text;
            user.CreatedDate = DateTime.Parse(txtCreateDate.Text);
            user.LastLogin = DateTime.Parse(txtLastLogin.Text);
            if (drActive.Text.Equals("Active"))
                user.IsActive = true;
            else
                user.IsActive = false;
            if (drBan.Text.Equals("Ban"))
                user.IsBan = false;
            else
                user.IsBan = true;
            if (drRoleName.Text.Equals("Member"))
                user.RoleID = 1;
            else
                user.RoleID = 2;
            string sort = "ASC";
            if (drSort.Text.Equals("Descending"))
                sort = "DESC";
            userData = new UserData();
            listUser = userData.Search(user, sort);
            if (listUser != null)
            {
                gvvFilterAcc.DataSource = listUser;
                gvvFilterAcc.DataBind();
                pnlViewDetailsAcc.Visible = false;
            }
            else
            {
                lblFilterAcc.Text = "No record for filter.";
            }


        }

        protected void btnFilterAdvence_Click(object sender, EventArgs e)
        {
            Search();
        }
    }
}